﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Meqs.Entity;
using Meqs.Logic.Interface;

public partial class School_Report_KindSchoolReport : System.Web.UI.Page
{
    protected  List<KindSettingsSchool> kindSettingsSchoolList = null;
    protected  List<ClassesKindSchool> classesKindSchoolList = null;
    protected  List<ClsInfo> viewData = null;
    //总计列表，表尾统计数据
    protected  List<int> totalizeList = new List<int>();
    protected  int currPage = 1;
    protected  int subjectID = 0;

    protected  int examId = 0;
    protected  int classType = -1;
    protected  int grade = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            currPage = 1;
        }
    }

    private void ReadCookies()
    {
        examId = SchoolCookie.ExamID;
        classType = SchoolCookie.ExamClassType;
        grade = SchoolCookie.ExamGrade;
    }

    protected void btnBind_Click(object sender, EventArgs e)
    {
        subjectID = Convert.ToInt32(subjectIDtxt.Text);
        ReadCookies();

        IKindSettingsSchoolLogic kindSettingsSchoolLogic = Container<IKindSettingsSchoolLogic>.Instance;
        IClassesKindSchoolLogic classesKindSchoolLogic = Container<IClassesKindSchoolLogic>.Instance;
        IClsInfoLogic clsLogic = Container<IClsInfoLogic>.Instance;

        //获取性质类型
        Hashtable ht1 = new Hashtable();
        ht1.Add("SchoolMark", CommonScore.GetSchoolMark);
        ht1.Add("ExamID", examId);
        ht1.Add("Grade", SchoolCookie.ExamGrade);
        ht1.Add("SubjectID", subjectID);
        kindSettingsSchoolList = (List<KindSettingsSchool>)kindSettingsSchoolLogic.FindAll(ht1);
        if (kindSettingsSchoolList == null) return;

        //获取班级列表
        Hashtable ht3 = new Hashtable();
        ht3.Add("SchoolMark", CommonScore.GetSchoolMark);
        ht3.Add("ExamID", examId);
        List<ClsInfo> list = (List<ClsInfo>)clsLogic.FindByExamClass_S(ht3);
        if (list == null) return;
        foreach (ClsInfo cls in list)
        {
            Hashtable ht = new Hashtable();
            ht.Add("ClassID", cls.RID);
            ht.Add("ExamID", examId);
            ht.Add("SchoolMark", CommonScore.GetSchoolMark);
            Reports.QueryExamCatch(0, Reports.CATCH_KINDSET_CLASS).CheckCatch(ht);
        }

        //获取分析数据
        Hashtable ht2 = new Hashtable();
        ht2.Add("SchoolMark", CommonScore.GetSchoolMark);
        ht2.Add("SchoolTestID", examId);
        ht2.Add("Type", classType);
        ht2.Add("Grade", grade);
        ht2.Add("Dirty", 0);
        classesKindSchoolList = (List<ClassesKindSchool>)classesKindSchoolLogic.FindAll(ht2);
        if (classesKindSchoolList == null) return;




        viewData = list;
        AspNetPager1.RecordCount = viewData.Count;

        //初始化表尾总计列表
        totalizeList.Clear();
        foreach (KindSettingsSchool kindSettingsSchool in kindSettingsSchoolList)
        {
            totalizeList.Add(0);
            totalizeList.Add(0);
        }

        Bind();
    }

    private void Bind()
    {
        int index = (currPage - 1) * AspNetPager1.PageSize;
        int count = 0;
        if (viewData.Count - index >= AspNetPager1.PageSize) count = AspNetPager1.PageSize;
        else count = viewData.Count - index;

        List<ClsInfo> pageList = viewData.GetRange(index, count);

        Repeater1.DataSource = pageList;
        Repeater1.DataBind();
    }

    //获取班级列表
    //[System.Web.Services.WebMethod]
    //public static void SetQueryParameter(int subject)
    //{
    //    if (subject >= 0)
    //        subjectID = subject;
    //}

    //获取表头的所有性质
    public string GetKindHeaderString()
    {
        string strTitle = "";
        foreach (KindSettingsSchool kindSettingsSchool in kindSettingsSchoolList)
        {
            strTitle += "<th width=\"6%\" style=\"text-align: center;padding-left:0px;\" colspan=\"2\">";
            strTitle += kindSettingsSchool.Name;
            strTitle += "</th>";
        }

        strTitle += "<tr>";
        foreach (KindSettingsSchool kindSettingsSchool in kindSettingsSchoolList)
        {
            strTitle += "<th width=\"3%\" style=\"text-align: center;padding-left:0px;\">";
            strTitle += "命中";
            strTitle += "</th>";

            strTitle += "<th width=\"3%\" style=\"text-align: center;padding-left:0px;\">";
            strTitle += "累计";
            strTitle += "</th>";
        }
        strTitle += "</tr>";

        return strTitle;
    }

    //获取分析数据
    public string GetKindInfoString(int classRID)
    {
        string strTitle = "";

        //列数异常
        if (totalizeList.Count != kindSettingsSchoolList.Count * 2)
            return strTitle;

        for (int index = 0; index < kindSettingsSchoolList.Count; ++index)
        {
            string strInNum = "";
            string strOverNum = "";

            foreach (ClassesKindSchool classesKindSchool in classesKindSchoolList)
            {
                if (classesKindSchool.ItemID == kindSettingsSchoolList[index].RID && classRID == classesKindSchool.ClassID)
                {
                    strInNum = classesKindSchool.InNum.ToString();
                    totalizeList[index * 2] += classesKindSchool.InNum;

                    strOverNum = classesKindSchool.OverNum.ToString();
                    totalizeList[index * 2 + 1] += classesKindSchool.OverNum;

                    break;
                }
            }

            strTitle += "<td style=\"text-align: right; font-weight: normal;padding-right:5px;\">";
            strTitle += strInNum;
            strTitle += "</td>";

            strTitle += "<td style=\"text-align: right; font-weight: normal;padding-right:5px;\">";
            strTitle += strOverNum;
            strTitle += "</td>";
        }

        return strTitle;
    }

    //获取表尾的统计数据
    public string GetTotalizeRowString()
    {
        string strTitle = "";

        //列数异常
        if (totalizeList.Count != kindSettingsSchoolList.Count * 2)
            return strTitle;

        for (int index = 0; index < kindSettingsSchoolList.Count; ++index)
        {
            strTitle += "<td style=\"text-align: right; font-weight: normal;padding-right:5px;font-weight:bold;\">";
            strTitle += totalizeList[index * 2].ToString();
            strTitle += "</td>";

            strTitle += "<td style=\"text-align: right; font-weight: normal;padding-right:5px;font-weight:bold;\">";
            strTitle += totalizeList[index * 2 + 1].ToString();
            strTitle += "</td>";
        }

        return strTitle;
    }
}
